package com.lytis.zoovideo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lytis.zoovideo.common.R;
import com.lytis.zoovideo.entity.Comment;
import com.lytis.zoovideo.entity.CommentAssUser;
import com.lytis.zoovideo.entity.User;
import com.lytis.zoovideo.entity.vo.CommentVo;
import com.lytis.zoovideo.service.CommentAssUserService;
import com.lytis.zoovideo.service.CommentService;
import com.lytis.zoovideo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :DongPeiLei
 * @description :
 * @create :2022-06-05 19:47:00
 */
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentAssUserService assService;

    @GetMapping
    @Transactional
    public R getCommentsByPagination(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
            , String videoId) {
        Page<Comment> commentPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Comment> commentWrapper = new QueryWrapper<Comment>();
        commentWrapper.orderByDesc("likes").orderByAsc("created_at").eq("video_id", videoId);
        commentService.page(commentPage, commentWrapper);
        /*评论Vo数组*/
        List<CommentVo> records = new ArrayList<>();
        for (Comment record : commentPage.getRecords()) {
            /*封装Vo数组*/
            CommentVo vo = new CommentVo();
            vo.setId(record.getId());
            vo.setContent(record.getContent());
            vo.setLikes(record.getLikes());
            vo.setCreatedAt(record.getCreatedAt());
            User user = userService.getById(record.getUserId());
            if (user == null) return R.error().message("用户不存在，评论失败");
            /*获取点赞列表*/
            QueryWrapper<CommentAssUser> assWrapper = new QueryWrapper<>();
            assWrapper.eq("comment_id", record.getId());
            List<CommentAssUser> assUsers = assService.list(assWrapper);
            List<Integer> likedUserList = new ArrayList<>();
            for (CommentAssUser assUser : assUsers) {
                likedUserList.add(assUser.getUserId());
            }
            vo.setLikedUserList(likedUserList);
            vo.setUserName(user.getUsername());
            records.add(vo);
        }
        return R.ok().data("results", records).data("total", commentPage.getTotal());
    }

    @PostMapping
    public R createComment(@RequestBody Comment comment) {
        boolean isSave = commentService.save(comment);
        return isSave ? R.ok().message("评论成功") : R.error().message("评论失败");
    }

    @PostMapping("/like")
    @Transactional
    public R changeLikes(@RequestBody Comment comment) {
        QueryWrapper<CommentAssUser> assWrapper = new QueryWrapper<>();
        assWrapper.eq("user_id", comment.getUserId()).eq("comment_id", comment.getId());
        CommentAssUser record = assService.getOne(assWrapper);
        QueryWrapper<Comment> commentWrapper = new QueryWrapper<>();
        commentWrapper.eq("id", comment.getId());
        if (record != null) {
            assService.removeById(record);
            comment.setLikes(comment.getLikes() - 1);
            commentService.update(comment, commentWrapper);
            return R.ok().message("取消点赞成功");
        } else {
            CommentAssUser assUser = new CommentAssUser();
            assUser.setUserId(comment.getUserId());
            assUser.setCommentId(comment.getId());
            assService.save(assUser);
            comment.setLikes(comment.getLikes() + 1);
            commentService.update(comment, commentWrapper);
            return R.ok().message("点赞成功");
        }
    }

}
